VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "SelectionCheckSize"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------------------------------------------
'
' [RelaxTools-Addin] v4
'
' Copyright (c) 2009 Yasuhiro Watanabe
' https://github.com/RelaxTools/RelaxTools-Addin
' author:relaxtools@opensquare.net
'
' The MIT License (MIT)
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'
'-----------------------------------------------------------------------------------------------------
Option Explicit

Dim lngSizeByte As Long
Dim lngSizeCol As Long
Dim rByte As Range
Dim rCol As Range
Public Enum SelectionCheckSizeConstants
    CountTypeSJIS
    CountTypeUTF8
    CountTypeUTF16
End Enum
Dim mCountType As SelectionCheckSizeConstants
'------------------------------------------------------------------------------------------------------------------------
' SelectionFrameWork 宣言
'------------------------------------------------------------------------------------------------------------------------
Private WithEvents SFWork As SelectionFrameWork
Attribute SFWork.VB_VarHelpID = -1

'------------------------------------------------------------------------------------------------------------------------
' SelectionFrameWork 作成
'------------------------------------------------------------------------------------------------------------------------
Private Sub Class_Initialize()
    Set SFWork = New SelectionFrameWork
End Sub

'------------------------------------------------------------------------------------------------------------------------
' SelectionFrameWork 開放
'------------------------------------------------------------------------------------------------------------------------
Private Sub Class_Terminate()
    Set SFWork = Nothing
End Sub

'------------------------------------------------------------------------------------------------------------------------
' SelectionFrameWork Run
'------------------------------------------------------------------------------------------------------------------------
Public Sub Run()
    SFWork.Run
End Sub

'------------------------------------------------------------------------------------------------------------------------
' 前処理(UOC)
'------------------------------------------------------------------------------------------------------------------------
Private Sub SFWork_SelectionInit(Cancel As Boolean, Undo As Boolean, func As Boolean)

    lngSizeByte = 0
    lngSizeCol = 0
    Undo = True
    func = True

End Sub
Public Property Let CountType(ByVal v As SelectionCheckSizeConstants)
    mCountType = v
End Property




Private Sub SFWork_SelectionMain(r As Range, ByVal NotHoldFormat As Boolean, Cancel As Boolean)

    On Error GoTo e
    
    Dim lngByte As Long
    Dim lngCol As Long
    Dim bytBuf() As Byte
    
    Select Case mCountType
        Case CountTypeSJIS
            lngByte = LenB(StrConv(r.Text, vbFromUnicode))
            
        Case CountTypeUTF8
            Dim U8 As UTF8Encoding
            Set U8 = New UTF8Encoding
            bytBuf = U8.getBytes(r.Text)
            lngByte = UBound(bytBuf()) - LBound(bytBuf()) + 1
            Set U8 = Nothing
            
        Case CountTypeUTF16
            lngByte = LenB(r.Text)
    End Select
'    lngCol = Len(r.Text)
    lngCol = LenEx(r.Text)
    
    If lngByte > lngSizeByte Then
        lngSizeByte = lngByte
        Set rByte = r
    End If
    
    If lngCol > lngSizeCol Then
        lngSizeCol = lngCol
        Set rCol = r
    End If

    Exit Sub
e:
    Call rlxErrMsg(Err)
    Cancel = True
    
End Sub

Private Sub SFWork_SelectionTerm()

    On Error GoTo e
    
    If rByte Is Nothing Then
    Else
        With rByte.Interior
            .Color = vbRed
            .Pattern = xlSolid
        End With
    End If

    If rCol Is Nothing Then
    Else
        With rCol.Interior
            .Color = vbYellow
            .Pattern = xlSolid
        End With
    End If

    frmMaxCount.Start lngSizeByte, lngSizeCol
    
    Exit Sub
e:
    Call rlxErrMsg(Err)

End Sub
